/* ****************************************************************************
stree.h - space tree.
-------------------------------------------------------------------------------

Copyright (c) 2017, Tain L.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

**************************************************************************** */
#if !defined(SPEARS_SPEARS_STREE_H)
#define SPEARS_SPEARS_STREE_H

#include "../my-prerequisites.h"
#include "../core/scenenode.h"

namespace Spears
{
	class SPEARS_API STree
	{
	public:
		typedef SceneNode NodeType;

	public:
		STree();
		STree(const STree& rhs);
		STree(STree&& rhs);
		virtual ~STree();


		STree& operator=(const STree& rhs);
		STree& operator=(STree&& rhs);
		bool operator==(const STree& rhs) const;

	public:
		virtual void set_root(const Pool<NodeType>::Addr& addr_root);

		/**
		@method: push
		@desc: push the node into this tree as a child node RECURSIVELY!
		*/
		virtual void push(const Pool<NodeType>::Addr& node);

		/**
		@method: push
		@desc: push a tree into this tree as a child tree.
		*/
		virtual void push(const STree& tree);
		virtual bool has_children() const;
		virtual void build_index() {}		
		virtual bool is_valid() const;
		virtual Pool<NodeType>::Addr root() const;

	protected:
		Pool<NodeType>::Addr	m_root;
	};

	inline STree::STree()
	{}

	inline STree::STree(const STree& rhs)
		: m_root(rhs.m_root)
	{}

	inline STree::STree(STree&& rhs)
		: m_root(std::move(rhs.m_root))
	{}

	inline STree::~STree()
	{}

	inline STree& STree::operator=(const STree& rhs)
	{
		m_root = rhs.m_root;
		return *this;
	}

	inline STree& STree::operator=(STree&& rhs)
	{
		m_root = std::move(rhs.m_root);
		return *this;
	}

	inline bool STree::operator==(const STree& rhs) const
	{
		return (m_root == rhs.m_root);
	}

	inline void STree::set_root(const Pool<NodeType>::Addr& addr_root)
	{
		m_root = addr_root;
	}

	inline void STree::push(const Pool<NodeType>::Addr& node)
	{
		if (node.is_valid())
		{
			STree subtree;
			for(auto& childschild : node.get_ref().get_children())
			{
				subtree.push(childschild);
			}

			if (subtree.has_children())
			{
				push(subtree);
			}
		}
		else
		{
			throw std::exception("invalid address");
		}
	}

	inline void STree::push(const STree& tree)
	{
		if (tree.m_root.is_valid())
		{
			m_root.get_ref().add_child(tree.m_root);
		}
	}

	inline bool STree::has_children() const
	{
		if (m_root.is_valid())
		{
			if (m_root.get_ref().get_children().size() > 0)
				return true;
			else
				return false;
		}
		else
			return false;
	}

	inline Pool<STree::NodeType>::Addr STree::root() const
	{
		return m_root;
	}

	inline bool STree::is_valid() const
	{
		return m_root.is_valid();
	}
}

#endif // ! SPEARS_SPEARS_STREE_H